package vn.mobiistar.videoapp.other;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.R.integer;
import android.util.Log;

/*
 * class for holding video's information object\
 */

public class Video {
	private String 	name;
	private String 	videoid;
	private String 	videoFullUrl;
	private int 	duration;
	private String 	durationString;
	private String 	description;
	private int 	cat_id;
	private int 	viewed;
	private String 	created;
	private int 	created_user;
	private int 	is_feature;
	private String 	catName;
	private String 	thumnallUrl;
	
	private int 	youtube_viewed;
	private float 	youtube_ratting;
	private String 	youtube_link;
	private int 	youtube_like;
	
	// type: youtube = 0
	private int 	type;
	private String 	keyword;
	
	public Video(){
		
	}
	public Video(String name, String cate, int viewed, int duration){
		this.name    	= name;
		this.catName 	= cate;
		this.viewed 	= viewed;
		setDuration(duration);
	}
	public void setCat_id(int cat_id) {
		this.cat_id = cat_id;
	}
	public void setCreated(String created) {
		this.created = created;
	}
	public void setCreated_user(int created_user) {
		this.created_user = created_user;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public void setDuration(int duration) {
		this.duration 		= duration;
		if(this.duration >= 3600)
			this.durationString = String.format("%02d:%02d:%02d", duration/3600, (duration%3600)/60, (duration%60));
		else this.durationString = String.format("%02d:%02d", (duration%3600)/60, (duration%60));
		 
	}
	public void setThumnallUrl(String thumnallUrl) {
		this.thumnallUrl = thumnallUrl;
	}
	public void setIs_feature(int is_feature) {
		this.is_feature = is_feature;
	}
	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setType(int type) {
		this.type = type;
	}
	public void setVideoFullUrl(String videoFullUrl) {
		this.videoFullUrl = videoFullUrl;
	}
	public void setVideoid(String videoid) {
		this.videoid = videoid;
	}
	public void setViewed(int viewed) {
		this.viewed = viewed;
	}
	public void setYoutubeLike(int youtube_like) {
		this.youtube_like = youtube_like;
	}
	public void setYoutubeLink(String youtube_link) {
		this.youtube_link = youtube_link;
	}
	public void setYoutubeRatting(float youtube_ratting) {
		this.youtube_ratting = youtube_ratting;
	}
	public void setYoutubeViewed(int youtube_viewed) {
		this.youtube_viewed = youtube_viewed;
	}
	public void setCatName(String catName) {
		this.catName = catName;
	}
	public String getThumnallUrl() {
		return thumnallUrl;
	}
	public String getDurationString() {
		return durationString;
	}
	public int getCat_id() {
		return cat_id;
	}
	public String getCreated() {
		return created;
	}
	public int getCreated_user() {
		return created_user;
	}
	public String getDescription() {
		return description;
	}
	public int getDuration() {
		return duration;
	}
	public int getIs_feature() {
		return is_feature;
	}
	public String getKeyword() {
		return keyword;
	}
	public String getName() {
		return name;
	}
	public int getType() {
		return type;
	}
	public String getVideoFullUrl() {
		return videoFullUrl;
	}
	public String getVideoid() {
		return videoid;
	}
	public int getViewed() {
		return viewed;
	}
	public String getViewedString(){
		return String.valueOf(getViewed());
	}
	public int getYoutubeLike() {
		return youtube_like;
	}
	public String getYoutubeLink() {
		return youtube_link;
	}
	public float getYoutubeRatting() {
		return youtube_ratting;
	}
	public String getYoutubeViewed() {
		String res  = "0";
		if(youtube_viewed <1000) res = String.valueOf(youtube_viewed);
		
		if(youtube_viewed >1000 && youtube_viewed <1000000) res = String.format("%3d.%03d",  youtube_viewed/1000, youtube_viewed%1000);
		if(youtube_viewed >1000000 && youtube_viewed <1000000000) res = String.format("%3d.%03d.%03d",  youtube_viewed/1000000,youtube_viewed/1000000, youtube_viewed%1000);
		
		return res;
	}
	
	public String getCatName() {
		return catName;
	}
	
	 public static ArrayList<Video> valueOf(JSONObject paramJSONObject) throws JSONException
	    {
	        ArrayList<Video> arrr =  new ArrayList<Video>();
	        JSONObject rootObject = paramJSONObject.getJSONObject("items");
	        String url =  rootObject.getString("imageUrl");
	        for (int i = 0; i < rootObject.length()-1; i++) {
	        	JSONObject item = rootObject.getJSONObject(String.valueOf(i));
	        	Video v = new Video();
	        	if(item.getString("cat_id") 	  != null && item.getString("cat_id") != "" ) v.setCat_id(item.getInt("cat_id"));
	        	if(item.getString("cat_name") 	  != null && item.getString("cat_name") != ""  ) v.setCatName(item.getString("cat_name"));
	        	if(item.getString("description")  != null && item.getString("description") != ""  ) v.setDescription(item.getString("description"));
	        	if(item.getString("duration") 	  != null && item.getString("duration") != "null"  ) v.setDuration(Integer.valueOf( item.getString("duration")));
	        	if(item.getString("keyword") 	  != null && item.getString("keyword") != ""  ) v.setKeyword(item.getString("keyword"));
	        	if(item.getString("name") 		  != null && item.getString("name") != ""  ) v.setName(item.getString("name"));
	        	v.setType(0);
	        	if(item.getString("videoid") != null && item.getString("videoid") != ""  ) v.setVideoid(item.getString("videoid"));
	        	if(item.getString("viewed") != null && item.getString("viewed") != ""  ) v.setViewed(item.getInt("viewed"));
	        	if(item.getString("thumb") != null && item.getString("thumb") != ""  ) v.setThumnallUrl(url+item.getString("thumb"));
	        	
	        	if(item.getString("youtube_link") != null && item.getString("youtube_link") != ""  ) v.setYoutubeLink(item.getString("youtube_link"));
	        	if(item.getString("youtube_like") != null && item.getString("youtube_like") != ""  ) v.setYoutubeLink(item.getString("youtube_like"));
	        	if(item.getString("youtube_ratting") != null && item.getString("youtube_ratting") != ""  ) v.setYoutubeRatting(item.getLong("youtube_ratting"));
	        	if(item.getString("youtube_viewed") != null && item.getString("youtube_viewed") != ""  ) v.setYoutubeViewed(item.getInt("youtube_viewed"));
	        	
	        	arrr.add(v);
			}
	        Log.v("video", arrr.size()+" loaded");
	        return arrr;
	    }
}
